<template>
  <div class="app-container flex-col minH">
    <div class="topline w100p bgwhite ptb20 radius10 pl10 pr20 justify-between">
      <div>
        <el-input v-model="city" style="width: 280px" placeholder="搜尋城市名稱..." @blur="search" :prefix-icon="Search" class="search" />


      </div>
      <div>
        <el-button class="btn2" @click="search()">查詢</el-button>
        <el-button class="btn1" @click="reset()">重置</el-button>
      </div>
    </div>
    <div class="mt15 radius10 hidden pb15 flex1 bgwhite  flex-col w100p">
      <div class="flex1 w100p ">
        <el-table :data="tableData" style="width: 100%" ref="multipleTableRef" @selection-change="handleSelectionChange" cell-class-name="tbcell" header-cell-class-name="hdcell">
          <el-table-column type="selection" width="55" />
          <el-table-column prop="id" label="ID" width="60" />
          <el-table-column prop="city" label="城市名稱" width="200" />
          <el-table-column prop="created_at" label="創建時間" min-width="300" />
          <el-table-column prop="updated_at" label="更新時間" min-width="300" sortable />
          <el-table-column prop="" label="操作" width="180" align="right" fixed="right">
            <template #default="scope">
              <span class=" pointer" @click="show(scope.row)">展示</span>
              <span class="plr5">|</span>
              <span class=" pointer" @click="edit(scope.row)">編輯</span>
              <span class="plr5">|</span>
              <span class=" pointer red" @click="toDel(scope.row)">刪除</span>


            </template>
          </el-table-column>
        </el-table>
      </div>

      <div class=" justify-between plr20 w100p mt15">
        <div class="align-center">
          <el-button class="btn1" @click="batchDel()">刪除</el-button>

          <el-button class="btn2" @click="toAdd()">新增</el-button>
        </div>
        <el-pagination v-model:current-page="currentPage" prev-text="上一頁" next-text="下一頁" :default-page-size="15" :background="true" layout="prev, pager, next, jumper" :total="total" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
      </div>
    </div>

  </div>
</template>

<script setup lang="ts">
import { Search } from '@element-plus/icons-vue'
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router';
import { ElTable } from 'element-plus'
import { cityList, deleteCity } from '@/api/city.js'
interface User {
  date: string
  name: string
  address: string
}
onMounted(() => {
  getCityList()
})

const idStr = ref('')
const getCityList = () => {
  cityList({
    city: city.value,
    page: currentPage.value
  }).then((res) => {
    console.log('城市列表', res)
    tableData.value = res.data.data
    total.value = res.data.total
  })
}
const city = ref('')
const multipleSelection = ref<User[]>([])
const multipleTableRef = ref<InstanceType<typeof ElTable>>()
const handleSelectionChange = (val: User[]) => {
  // multipleSelection.value = val
  let idList = val.map(item => { return item.id })
  console.log(val)
  console.log(JSON.parse(JSON.stringify(idList)))
  idStr.value = idList.join(',')


}

const tableData = ref([])
const tableRowClassName = () => {
  return 'tbrow'
}
const currentPage = ref(1)
const total = ref(0)
const handleSizeChange = (val) => {
  console.log(`${val} items per page`)
}

const handleCurrentChange = (val) => {
  console.log(`current page: ${val}`)
  getCityList()
}
const search = () => {
  getCityList()
}
const reset = () => {
  city.value = ''
  getCityList()
}
const router = useRouter()
const batchDel = () => {
  deleteCity({
    id: idStr.value
  }).then((res) => {
    console.log('删除城市', res)
    getCityList()
  })
}
// 删除
const toDel = (row) => {
  deleteCity({
    id: row.id
  }).then((res) => {
    console.log('删除城市', res)
    getCityList()
  })
}
const toAdd = () => {
  console.log(router)
  router.push({ path: 'cityAdd' })
}
const edit = (row) => {
  router.push({ path: 'cityEdit', query: { ...row, editType: 'edit' } })
}
const show = (row) => {
  router.push({ path: 'cityEdit', query: { ...row, editType: 'show' } })
}
</script>

<style lang="scss" scoped>
.search {
  overflow: hidden;
  border-radius: 10px !important;
  // padding: 5px 0px;
}
</style>
<style></style>
